using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Developex.GenerateClassOnShema.Classes;

namespace Developex.GenerateClassOnShema
{
	static class Program
	{

		private const Int32 SW_HIDE = 0;
		private const Int32 SW_SHOW = 1;

		[DllImport("Kernel32.dll")]
		private static extern IntPtr GetConsoleWindow();

		[DllImport("user32.dll")]
		private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			bool isSql2000 = false;
			if (args.Length > 1 && args[args.Length - 1].ToUpper() == "SQL2000")
			{
				string[] args_ = new string[args.Length - 1];
				Array.Copy(args, args_, args.Length - 1);
				isSql2000 = true;
				args = args_;
			}
			bool ce = false;
			if (args.Length > 1 && args[args.Length - 1].ToUpper() == "CE")
			{
				string[] args_ = new string[args.Length - 1];
				Array.Copy(args, args_, args.Length - 1);
				ce = true;
				args = args_;
			}
			bool skipEncoded = false;
			if (args.Length > 1 && args[args.Length - 1] == "SkipEncoded")
			{
				string[] args_ = new string[args.Length - 1];
				Array.Copy(args, args_, args.Length - 1);
				skipEncoded = true;
				args = args_;
			}

			if (args.Length == 0 || (args.Length == 1 && args[0] == "?"))
			{
				WriteUsage();
			}
			else if (args.Length > 5)
			{
				Console.WriteLine("Incorrect number of parameters!");
				WriteUsage();
			}
			else if (args.Length == 1 && args[0] == "*")
			{
				IntPtr hwnd = GetConsoleWindow();
				ShowWindow(hwnd, SW_HIDE);

				Application.EnableVisualStyles();
				Application.SetCompatibleTextRenderingDefault(false);
				Application.Run(new MainForm());
				ShowWindow(hwnd, SW_SHOW);
			}
			else
			{
				string connectionString = args[0];
				string outputFolder = args.Length < 2 ? (ce ? @"DataLayerCE\Objects" : @"DataLayer\Objects") : args[1];
				string iniFileName = args.Length < 3 ? (ce ? "GenerateCE.ini" : "Generate.ini") : args[2];
				string indentSize = "   ";
				if (args.Length >= 4)
				{
					switch (args[3])
					{
						case "tabs":
							indentSize = "\t";
							break;
						case "space1":
							indentSize = " ";
							break;
						case "space2":
							indentSize = "  ";
							break;
						case "space3":
							indentSize = "   ";
							break;
						case "space4":
							indentSize = "    ";
							break;
					}
				}
				string nameSpace = args.Length < 5 ? (ce ? "DataLayerCE.Objects" : "DataLayer.Objects") : args[4];

				Generator.GenerateClassesForDatabase(iniFileName, outputFolder, connectionString, nameSpace, indentSize, ce, !skipEncoded, isSql2000);
			}
		}

		private static void WriteUsage()
		{
			string programFileName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
			Console.WriteLine("");
			Console.WriteLine("Syntax usage :");
			Console.WriteLine(programFileName + " (connectionString|database name) [outputFolder [iniFileName [indent [namespace]]]] [SkipEncoded] [CE] [sql2000]");
			Console.WriteLine("");
			Console.WriteLine(@"Note: if database name is used instead of connection string then connection string will be formatted automatically with Server=.\SQLExpress and Integrated Security=True");
			Console.WriteLine("Specify 'sql2000' if used database connection is MSSQL 2000");
			Console.WriteLine("Specify 'CE' if you want to generate compact edition data-layer(for PocketPC)");
			Console.WriteLine("Specify 'SkipEncoded' if you don't want to generate Encoded properties");
			Console.WriteLine("By default:");
			Console.WriteLine("outputFolder = DataLayer\\Objects | DataLayerCE\\Objects");
			Console.WriteLine("iniFileName = Generate.ini | GenerateCE.ini");
			Console.WriteLine("indent = space3 (tabs|space1|space2|space3|space4)");
			Console.WriteLine("namespace = DataLayer.Objects | DataLayerCE.Objects");
			Console.WriteLine("");
			Console.WriteLine("For GUI use: " + programFileName + " *");
		}
	}
}